#include "MyString.h"
#include <string.h>
#include <stdio.h>
#include <stdbool.h>

void InitMyString(MyStr *s)
{
    s->len = 0; // 初始化
    memset(s->str, 0, Size);
}

// 替代原来的字符串
void InsertStr(MyStr *s, const char *newStr)
{
    int slen = strlen(newStr);
    s->len = Size - 1 > slen ? slen : Size - 1;
    strncpy(s->str, newStr, Size - 1);
}

void AppendStr(MyStr *s, const char *newStr)
{
    strncat(s->str, newStr, Size - 1 - s->len);
    s->len = strlen(s->str);
}

void PrintStr(MyStr *s, bool HaveEnter)
{
    if (HaveEnter)
    {
        printf("%s\n", s->str);
    }
    else
    {
        printf("%s", s->str);
    }
}

void RemoveString(MyStr *s, const char *str)
{
    char *dest = strstr(s->str, str);
    while (dest != NULL)
    {
        char *back = dest + strlen(str);
        while (*back != '\0')
        {
            *dest++ = *back++;
        }
        *dest = '\0';
        dest = strstr(s->str, str);
    }
    s->len = strlen(s->str);
}

bool IsStrEqual(MyStr *s, MyStr *str)
{
    return strcmp(s->str, str->str) == 0;
}

char *GetSPtr(MyStr *s)
{
    return s->str;
}

void GetStr(MyStr *s)
{
    InitMyString(s);
    char c = 0;
    //读到回车结束
    while ((c = getchar()) != '\n' && s->len < Size - 1)
    {
        s->str[s->len++]=c;
    }
}
